<?php
namespace plugins\Emailer\controller;
use app\common\controller\Common;
require_once(dirname(dirname(__FILE__))."/sdk/PHPMailer.php");
/**
 * Created by PhpStorm.
 * User: wem
 * Date: 2017/2/21
 * Time: 16:10
 */
class Emailer extends Common
{
    public function send($data=[])
    {
        $config = plugin_config('Emailer');
        $validate = new Validate([
            ['email','require|email','邮箱输入错误|邮箱输入错误'],
            ['title','require','请输入邮件标题'],
            ['message','require','请输入邮件内容'],
        ]);
        if (!$validate->check($data)) {
            return $validate->getError();
        }
        $phpmailer = new \PHPMailer();
        $phpmailer->Host		=	$config['host']; //smtp服务器的名称（这里以QQ邮箱为例）
        $phpmailer->SMTPAuth 	= 	TRUE; //启用smtp认证
        $phpmailer->Username 	= 	$config['mail_user']; //你的邮箱名
        $phpmailer->Password 	= 	$config['password']; //邮箱密码
        $phpmailer->From 		= 	$config['from']; //发件人地址（也就是你的邮箱地址）
        $phpmailer->FromName 	=	$config['fromname']; //发件人姓名
        $phpmailer->CharSet		=	'utf-8'; //设置邮件编码
        $phpmailer->Subject 	=	$data['title']; //邮件主题
        $phpmailer->Body 		=	$data['message']; //邮件内容
        $phpmailer->AltBody 	=	$config['altbody']; //邮件正文不支持HTML的备用显示
        $phpmailer->WordWrap 	=	50; //设置每行字符长度
        $phpmailer->IsSMTP(true);	 // 启用SMTP
        $phpmailer->IsHTML(true); 	// 是否HTML格式邮件
        $phpmailer->AddAddress($data['email']);
        $status = $phpmailer->Send();
        return true;
    }
}